<template>
    <el-row class="container" type="flex" justify="space-between">
        <el-row class="mapWapper">
            <div id="map-container"></div>
        </el-row>
        <el-row class="locationWapper">
            <el-tabs v-model="type" @tab-click="handleTabClick">
                <el-tab-pane label="风景" name="风景">
                    <el-row class="location-item" type="flex" justify="space-between"
                    v-for="(item,index) in positions"
                    :key="index"
                    @mouseover.native="setLocationMap(item.id)"
                    @mouseout.native="discardLocationMap(item.id)"
                    >
                        <el-row class="location">{{item.name}}</el-row>
                        <el-row class="distance">{{item.distance}}公里</el-row>
                    </el-row>
                </el-tab-pane>
                <el-tab-pane label="交通" name="交通">
                    <el-row class="location-item" type="flex" justify="space-between"
                    v-for="(item,index) in positions"
                    :key="index"
                    @mouseover.native="setLocationMap(item.id)"
                    @mouseout.native="discardLocationMap(item.id)"
                    >
                        <el-row class="location">{{item.name}}</el-row>
                        <el-row class="distance">{{item.distance}}公里</el-row>
                    </el-row>
                </el-tab-pane>
            </el-tabs>
        </el-row>
    </el-row>
</template>

<script>
export default {
    data(){
        return{
            map:{},
            city:'',
            type:'风景',
            key:'829ed92b9947deb02e2da860197db068',
            positions:[],
            markerList:[]
        }
    },
    methods:{
        createMap(){
            this.map = new AMap.Map('map-container',{
                zoom:22,
                center: [113.32459, 23.10668]//中心点坐标
            });
        },
        // getCityByIp(){
        //     this.$axios({
        //         url:'https://restapi.amap.com/v3/ip',
        //         params:{
        //             key:this.key
        //         }
        //     }).then(res => {
        //         this.city = res.data.city.replace(/市$/,'');
        //     })
        // },
        getLocationsByType(){
            this.$axios({
                url:'https://restapi.amap.com/v3/place/around',
                params:{
                    key:this.key,
                    location: '113.324590,23.106681',
                    types:this.type
                }
            }).then(res => {
                this.positions = res.data.pois;
                this.positions.forEach(item => {
                    item.distance = (item.distance/1000).toFixed(2);
                });
                
                let map = this.map;
                map.clearMap();  // 清除地图覆盖物
                this.positions.forEach((item,index) => {
                    let marker = new AMap.Marker({
                        map:map,
                        icon:new AMap.Icon({            
                            image: 'https://webapi.amap.com/theme/v1.3/markers/b/mark_b.png',
                            size: new AMap.Size(38, 60),  //图标大小
                            imageSize: new AMap.Size(22, 36)
                        }),        
                        position: [item.location.split(',')[0], item.location.split(',')[1]],
                        extData:item.id
                    });
                    // 设置覆盖物
                    marker.setTop();
                    // 显示一个提示窗
                    const infoWin = new AMap.InfoWindow({
                        content:`<span style="font-size:12px;color:#333;">${item.name}</span>`,
                        offset:new AMap.Pixel(0,-32)
                    });
                    marker.setLabel({
                        content:index + 1
                    })

                    // 给marker添加悬浮事件
                    AMap.event.addListener(marker,'mouseover',function(e){
                        if(marker.getExtData() == e.target.Ce.extData){
                            infoWin.open(map,marker.getPosition());
                            map.setCenter(marker.getPosition());//设置中心点
                        }
                    });
                    AMap.event.addListener(marker,'mouseout',function(e){
                        if(marker.getExtData() == e.target.Ce.extData){
                            infoWin.close(map,marker.getPosition());
                            map.setCenter(marker.getPosition());
                        }
                    });
                   
                    //
                    this.markerList.push(marker);
                })
                // 让地图自适应显示所有点
                map.setFitView();
            })
        },
        handleTabClick(tab){
           this.type=tab.label;
           this.getLocationsByType();
        },
        setLocationMap(id){
            this.markerList.forEach(item => {
                if(item.getExtData() == id){
                    console.log(item);
                    item.showInfoWindow();
                    this.map.setCenter(item.getPosition());
                }
            });
        },
        discardLocationMap(id){
            this.markerList.forEach(item => {
                if(item.getExtData() == id){
                    item.clearInfoWindow();
                }
            });
        }
    },
    mounted(){
        this.createMap();
        this.getLocationsByType();
    }
}
</script>

<style lang="less" scoped>
.container{
    width: 1000px;
    height: 361px;
    margin:auto;
    .mapWapper{
        width: 654px;
        height:100%;
        #map-container{
            width:100%;
            height:100%;
        }
    }
    .locationWapper{
        width: 330px;
        height:100%;
        .location-item{
            padding:0 10px;
            height: 40px;
            line-height: 40px;
            font-size: 14px;
            color: #666666;
            cursor: pointer;
        }
    }
}
/deep/.el-tabs__content{
    max-height:300px;
    overflow-y: auto;
}
#map-container /deep/.amap-marker-label{
    border:0;
    background: transparent;
 }
#map-container /deep/.amap-marker-label{
    width:22px;
    border:0 none; 
    background: transparent;
    text-align: center;
    //padding:0 5px 0 4px;
    line-height: 20px;
    color: #fff;
    font-size: 16px;
    font-weight: 450;
    transform: translate(-3px,-2px);//平移
}
</style>